package learnjava.basic.other;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class TestEncode {

    public static void main(String[] args) {
        write();
    }


    public static void testGetBytes(){
        String chi = "王陈", eng = "eg";
        try {
            System.out.println(chi.getBytes("UTF-8").length);
            System.out.println(eng.getBytes("UTF-8").length);
            System.out.println(chi.getBytes("UTF-16").length);
            System.out.println(eng.getBytes("UTF-16").length);
            System.out.println(chi.getBytes("GBK").length);
            System.out.println(eng.getBytes("GBK").length);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static void write(){

        try {
            FileOutputStream fis = new FileOutputStream("D:\\test.txt");
            OutputStreamWriter osw = new OutputStreamWriter(fis,"UTF-16");
            osw.write("伟大汉字伟大汉字");   //18字节
//            osw.write("zhong");  //12字节
            /*不是说UTF-16编码无论中文英文字符都是两个字节吗，为什么编码后总是多两个字节呢？
            这是因为需要一个专门的字符来加在实际内容的前面，
            它就是BOM（Byte Order Mark）字节顺序标记，来告诉使用者我这个内容里的字节顺序是高位在前还是低位在前的。
            FEFF:Big-Endian，FFFE：Little-Endian
            */
            osw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
